/**
 * 通用数据方法脚本
 */

module.exports = {
  version: '1.0.0', // 游戏版本号
  url: 'https://xcx.nanxuncn.com/api/jump/', // http请求url
  gh_id: 'gh_aefebf6aba83', // 微信小游戏原始id
  canRevive: true, // 能否复活
  money: 300, // 金币
  gameIsStart: false, // 游戏是否开始
  openid: null, // 微信用户openid
  user_id: 0, // 微信用户id
  max_score: 0, // 最高分
  bgm_play: true, // bgm播放状态
  first_play: true, // 是否第一次玩
  share_flag: false, // 分享flag
  isWechatGame: true, // 是否是微信小游戏
  canShowAd: true, // 能否展示广告
  windowHeight: null, // 屏幕高度
  windowWidth: null, // 屏幕宽度
  bannerAd: null, // banner广告实例
  videoAd: null, // 视频广告实例
  bannerAdId: 'adunit-d20e1c7f043191c8', // banner广告id
  videoAdId: 'adunit-31ba2ee06b88df67', // 视频广告id
  moreAppId: 'wx3d3d7bd50ac38ff1', // 更多好玩跳转小程序appid
  shareTitle: '快来和我一起蹦蹦Jump吧!',
  shareImgUrl: 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jumpjump/share/1.jpg', // 分享图片
  gameOverSecond: 0.33, // 台阶掉落最小时间
  stairDownSecond: 4, // 初始台阶掉落时间
  hinderRate: 0.3, // 障碍物出现概率
  coinRate: 0.1, // 金币出现概率

  // 最高分操作
  handlerMaxScore: function () {
    var score = cc.sys.localStorage.getItem("score");
    var max_score = cc.sys.localStorage.getItem("max_score");

    if (score - max_score > 0) {
      cc.sys.localStorage.setItem("max_score", score);
      // 将最高分传给微信服务器
      if (this.isWechatGame) {
        var kvDataList = new Array();
        kvDataList.push({ key: "max_score", value: score.toString() });
        wx.setUserCloudStorage({
          KVDataList: kvDataList,
          success: function (res) {
            console.log(res);
          },
          fail: function (res) {
            console.log(res);
          }
        });
        // 将最高分传给服务器
        wx.request({
          url: this.url + 'accs/save_max_score/openid/' + this.openid + '/max_score/' + score,
          success: function (res) {
            console.log(res);
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  },

  // 处理金钱方法
  handlerMonry: function () {
    if (this.isWechatGame) {
      wx.request({
        url: this.url + 'accs/update_money/openid/' + this.openid + '/money/' + this.money,
        success: function (res) {
          console.log(res);
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  // 比较两个版本号大小
  compareVersion: function (v1, v2) {
    v1 = v1.split(".");
    v2 = v2.split(".");
    var len = Math.max(v1.length, v2.length);
    while (v1.length < len) {
      v1.push('0');
    }
    while (v2.length < len) {
      v2.push('0');
    }
    for (var i = 0; i < len; i++) {
      var n1 = parseInt(v1[i]);
      var n2 = parseInt(v2[i]);
      if (n1 > n2) return 1;
      if (n1 < n2) return -1;
    }
    return 0;
  },

  // 创建banner广告
  _createBannerAd: function () {
    if (!this.canShowAd) return;
    var sysInfo = wx.getSystemInfoSync();
    this.windowHeight = sysInfo.windowHeight;
    this.windowWidth = sysInfo.windowWidth;
    var that = this;
    var bannerH = 80;
    var bannerW = 350;
    if (!that.bannerAd) {
      that.bannerAd = wx.createBannerAd({
        adUnitId: that.bannerAdId,
        style: {
          left: (sysInfo.windowWidth - bannerW) / 2,
          top: sysInfo.windowHeight - bannerH,
          width: bannerW,
        }
      });
    }
  },
  // 释放banner广告
  _releaseBannerAd: function () {
    if (!this.canShowAd) return;
    if (this.bannerAd) {
      this.bannerAd.destroy();
    }
  },
  // 展示banner广告
  _showBannerAd: function () {
    if (!this.canShowAd) return;
    var that = this;
    that.bannerAd.show().catch(err => console.log("banner广告展示失败" + err));
    // 微信缩放后得到banner的真实高度,从新设置banner的top属性
    that.bannerAd.onResize(res => {
      that.bannerAd.style.top = that.windowHeight - that.bannerAd.style.realHeight;
      that.bannerAd.style.left = (that.windowWidth - that.bannerAd.style.realWidth) / 2;
    });
    that.bannerAd.onError(error => {
      console.log(error);
    });
  },
  // 隐藏banner广告
  _hideBannerAd: function () {
    if (!this.canShowAd) return;
    this.bannerAd.hide()
  },

  // 创建视频广告
  _createVideoAd: function (self) {
    if (!this.canShowAd) return;
    var that = this;
    if (!that.videoAd) {
      that.videoAd = wx.createRewardedVideoAd({adUnitId: that.videoAdId});
    }
    that.videoAd.bind_this = self;
    that.videoAd.onClose(that.videoAd.bind_this.adSuccessPlay);
  },
  // 释放视频广告
  _releaseVideoAd: function () {
    if (!this.canShowAd) return;
    var that = this
    if (that.videoAd && that.videoAd.bind_this) {
      that.videoAd.offClose(that.videoAd.bind_this.adSuccessPlay);
      that.videoAd.bind_this = null;
    }
  },
  // 加载并播放视频广告
  _loadVideoAd: function () {
    if (!this.canShowAd) return;
    var that = this;
    that.videoAd.load().then(() => that.videoAd.show()).catch(err => {
      console.log(err);
      wx.showToast({
        title: '视频广告获取失败,请稍后重试',
        icon: 'none',
        duration: 3000
      });
    });
  },

  // http GET请求
  httpGets: function (url, callback) {
    var xhr = cc.loader.getXMLHttpRequest();
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
        var respone = xhr.responseText;
        callback(respone);
      }
    };
    xhr.open("GET", url, true);
    if (cc.sys.isNative) {
      xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
    }

    // note: In Internet Explorer, the timeout property may be set only after calling the open()
    // method and before calling the send() method.
    xhr.timeout = 5000;// 5 seconds for timeout

    xhr.send();
  },
};